home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / pl4019ax.zip / PERLDB.PL < prev    next >
Perl Script  |  1991-11-13  |  17KB  |  582 lines

  1. package DB;
  2.  
  3. # modified Perl debugger, to be run from Emacs in perldb-mode
  4. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  5. # Johan Vromans -- upgrade to 4.0 pl 10
  6.  
  7. $header = '$RCSfile: perldb.pl,v $$Revision: 4.0.1.2 $$Date: 91/11/05 17:55:58 $';
  8. #
  9. # This file is automatically included if you do perl -d.
  10. # It's probably not useful to include this yourself.
  11. #
  12. # Perl supplies the values for @line and %sub.  It effectively inserts
  13. # a do DB'DB(<linenum>); in front of every place that can
  14. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  15. #
  16. # $Log:    perldb.pl,v $
  17. # Revision 4.0.1.2  91/11/05  17:55:58  lwall
  18. # patch11: perldb.pl modified to run within emacs in perldb-mode
  19. # Revision 4.0.1.1  91/06/07  11:17:44  lwall
  20. # patch4: added $^P variable to control calling of perldb routines
  21. # patch4: debugger sometimes listed wrong number of lines for a statement
  22. # Revision 4.0  91/03/20  01:25:50  lwall
  23. # 4.0 baseline.
  24. # Revision 3.0.1.6  91/01/11  18:08:58  lwall
  25. # patch42: @_ couldn't be accessed from debugger
  26. # Revision 3.0.1.5  90/11/10  01:40:26  lwall
  27. # patch38: the debugger wouldn't stop correctly or do action routines
  28. # Revision 3.0.1.4  90/10/15  17:40:38  lwall
  29. # patch29: added caller
  30. # patch29: the debugger now understands packages and evals
  31. # patch29: scripts now run at almost full speed under the debugger
  32. # patch29: more variables are settable from debugger
  33. # Revision 3.0.1.3  90/08/09  04:00:58  lwall
  34. # patch19: debugger now allows continuation lines
  35. # patch19: debugger can now dump lists of variables
  36. # patch19: debugger can now add aliases easily from prompt
  37. # Revision 3.0.1.2  90/03/12  16:39:39  lwall
  38. # patch13: perl -d didn't format stack traces of *foo right
  39. # patch13: perl -d wiped out scalar return values of subroutines
  40. # Revision 3.0.1.1  89/10/26  23:14:02  lwall
  41. # patch1: RCS expanded an unintended $Header in lib/perldb.pl
  42. # Revision 3.0  89/10/18  15:19:46  lwall
  43. # 3.0 baseline
  44. # Revision 2.0  88/06/05  00:09:45  root
  45. # Baseline version 2.0.
  46. #
  47.  
  48. open(IN, "</dev/tty") || open(IN,  "<&STDIN");    # so we don't dingle stdin
  49. open(OUT,">/dev/tty") || open(OUT, ">&STDOUT");    # so we don't dongle stdout
  50. select(OUT);
  51. $| = 1;                # for DB'OUT
  52. select(STDOUT);
  53. $| = 1;                # for real STDOUT
  54. $sub = '';
  55.  
  56. # Is Perl being run from Emacs?
  57. $emacs = $main'ARGV[$[] eq '-emacs';
  58. shift(@main'ARGV) if $emacs;
  59.  
  60. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  61. print OUT "\nLoading DB routines from $header\n";
  62. print OUT ("Emacs support ",
  63.        $emacs ? "enabled" : "available",
  64.        ".\n");
  65. print OUT "\nEnter h for help.\n\n";
  66.  
  67. sub DB {
  68.     &save;
  69.     ($package, $filename, $line) = caller;
  70.     $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
  71.     "package $package;";        # this won't let them modify, alas
  72.     local($^P) = 0;            # don't debug our own evals
  73.     local(*dbline) = "_<$filename";
  74.     $max = $#dbline;
  75.     if (($stop,$action) = split(/\0/,$dbline{$line})) {
  76.     if ($stop eq '1') {
  77.         $signal |= 1;
  78.     }
  79.     else {
  80.         $evalarg = "\$DB'signal |= do {$stop;}"; &eval;
  81.         $dbline{$line} =~ s/;9($|\0)/$1/;
  82.     }
  83.     }
  84.     if ($single || $trace || $signal) {
  85.     if ($emacs) {
  86.         print OUT "\032\032$filename:$line:0\n";
  87.     } else {
  88.         print OUT "$package'" unless $sub =~ /'/;
  89.         print OUT "$sub($filename:$line):\t",$dbline[$line];
  90.         for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  91.         last if $dbline[$i] =~ /^\s*(}|#|\n)/;
  92.         print OUT "$sub($filename:$i):\t",$dbline[$i];
  93.         }
  94.     }
  95.     }
  96.     $evalarg = $action, &eval if $action;
  97.     if ($single || $signal) {
  98.     $evalarg = $pre, &eval if $pre;
  99.     print OUT $#stack . " levels deep in subroutine calls!\n"
  100.         if $single & 4;
  101.     $start = $line;
  102.       CMD:
  103.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  104.         {
  105.         $single = 0;
  106.         $signal = 0;
  107.         $cmd eq '' && exit 0;
  108.         chop($cmd);
  109.         $cmd =~ s/\\$// && do {
  110.             print OUT "  cont: ";
  111.             $cmd .= &gets;
  112.             redo CMD;
  113.         };
  114.         $cmd =~ /^q$/ && exit 0;
  115.         $cmd =~ /^$/ && ($cmd = $laststep);
  116.         push(@hist,$cmd) if length($cmd) > 1;
  117.         ($i) = split(/\s+/,$cmd);
  118.         eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  119.         $cmd =~ /^h$/ && do {
  120.             print OUT "
  121. T        Stack trace.
  122. s        Single step.
  123. n        Next, steps over subroutine calls.
  124. r        Return from current subroutine.
  125. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  126.         at the specified line.
  127. <CR>        Repeat last n or s.
  128. l min+incr    List incr+1 lines starting at min.
  129. l min-max    List lines.
  130. l line        List line;
  131. l        List next window.
  132. -        List previous window.
  133. w line        List window around line.
  134. l subname    List subroutine.
  135. f filename    Switch to filename.
  136. /pattern/    Search forwards for pattern; final / is optional.
  137. ?pattern?    Search backwards for pattern.
  138. L        List breakpoints and actions.
  139. S        List subroutine names.
  140. t        Toggle trace mode.
  141. b [line] [condition]
  142.         Set breakpoint; line defaults to the current execution line; 
  143.         condition breaks if it evaluates to true, defaults to \'1\'.
  144. b subname [condition]
  145.         Set breakpoint at first line of subroutine.
  146. d [line]    Delete breakpoint.
  147. D        Delete all breakpoints.
  148. a [line] command
  149.         Set an action to be done before the line is executed.
  150.         Sequence is: check for breakpoint, print line if necessary,
  151.         do action, prompt user if breakpoint or step, evaluate line.
  152. A        Delete all actions.
  153. V [pkg [vars]]    List some (default all) variables in package (default current).
  154. X [vars]    Same as \"V currentpackage [vars]\".
  155. < command    Define command before prompt.
  156. > command    Define command after prompt.
  157. ! number    Redo command (default previous command).
  158. ! -number    Redo number\'th to last command.
  159. H -number    Display last number commands (default all).
  160. q or ^D        Quit.
  161. p expr        Same as \"print DB'OUT expr\" in current package.
  162. = [alias value]    Define a command alias, or list current aliases.
  163. command        Execute as a perl statement in current package.
  164.  
  165. ";
  166.             next CMD; };
  167.         $cmd =~ /^t$/ && do {
  168.             $trace = !$trace;
  169.             print OUT "Trace = ".($trace?"on":"off")."\n";
  170.             next CMD; };
  171.         $cmd =~ /^S$/ && do {
  172.             foreach $subname (sort(keys %sub)) {
  173.             print OUT $subname,"\n";
  174.             }
  175.             next CMD; };
  176.         $cmd =~ s/^X\b/V $package/;
  177.         $cmd =~ /^V$/ && do {
  178.             $cmd = 'V $package'; };
  179.         $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  180.             $packname = $1;
  181.             @vars = split(' ',$2);
  182.             do 'dumpvar.pl' unless defined &main'dumpvar;
  183.             if (defined &main'dumpvar) {
  184.             &main'dumpvar($packname,@vars);
  185.             }
  186.             else {
  187.             print DB'OUT "dumpvar.pl not available.\n";
  188.             }
  189.             next CMD; };
  190.         $cmd =~ /^f\b\s*(.*)/ && do {
  191.             $file = $1;
  192.             if (!$file) {
  193.             print OUT "The old f command is now the r command.\n";
  194.             print OUT "The new f command switches filenames.\n";
  195.             next CMD;
  196.             }
  197.             if (!defined $_main{'_<' . $file}) {
  198.             if (($try) = grep(m#^_<.*$file#, keys %_main)) {
  199.                 $file = substr($try,2);
  200.                 print "\n$file:\n";
  201.             }
  202.             }
  203.             if (!defined $_main{'_<' . $file}) {
  204.             print OUT "There's no code here anything matching $file.\n";
  205.             next CMD;
  206.             }
  207.             elsif ($file ne $filename) {
  208.             *dbline = "_<$file";
  209.             $max = $#dbline;
  210.             $filename = $file;
  211.             $start = 1;
  212.             $cmd = "l";
  213.             } };
  214.         $cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
  215.             $subname = $1;
  216.             $subname = "main'" . $subname unless $subname =~ /'/;
  217.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  218.             ($file,$subrange) = split(/:/,$sub{$subname});
  219.             if ($file ne $filename) {
  220.             *dbline = "_<$file";
  221.             $max = $#dbline;
  222.             $filename = $file;
  223.             }
  224.             if ($subrange) {
  225.             if (eval($subrange) < -$window) {
  226.                 $subrange =~ s/-.*/+/;
  227.             }
  228.             $cmd = "l $subrange";
  229.             } else {
  230.             print OUT "Subroutine $1 not found.\n";
  231.             next CMD;
  232.             } };
  233.         $cmd =~ /^w\b\s*(\d*)$/ && do {
  234.             $incr = $window - 1;
  235.             $start = $1 if $1;
  236.             $start -= $preview;
  237.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  238.         $cmd =~ /^-$/ && do {
  239.             $incr = $window - 1;
  240.             $cmd = 'l ' . ($start-$window*2) . '+'; };
  241.         $cmd =~ /^l$/ && do {
  242.             $incr = $window - 1;
  243.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  244.         $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  245.             $start = $1 if $1;
  246.             $incr = $2;
  247.             $incr = $window - 1 unless $incr;
  248.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  249.         $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  250.             $end = (!$2) ? $max : ($4 ? $4 : $2);
  251.             $end = $max if $end > $max;
  252.             $i = $2;
  253.             $i = $line if $i eq '.';
  254.             $i = 1 if $i < 1;
  255.             if ($emacs) {
  256.             print OUT "\032\032$filename:$i:0\n";
  257.             $i = $end;
  258.             } else {
  259.             for (; $i <= $end; $i++) {
  260.                 print OUT "$i:\t", $dbline[$i];
  261.                 last if $signal;
  262.             }
  263.             }
  264.             $start = $i;    # remember in case they want more
  265.             $start = $max if $start > $max;
  266.             next CMD; };
  267.         $cmd =~ /^D$/ && do {
  268.             print OUT "Deleting all breakpoints...\n";
  269.             for ($i = 1; $i <= $max ; $i++) {
  270.             if (defined $dbline{$i}) {
  271.                 $dbline{$i} =~ s/^[^\0]+//;
  272.                 if ($dbline{$i} =~ s/^\0?$//) {
  273.                 delete $dbline{$i};
  274.                 }
  275.             }
  276.             }
  277.             next CMD; };
  278.         $cmd =~ /^L$/ && do {
  279.             for ($i = 1; $i <= $max; $i++) {
  280.             if (defined $dbline{$i}) {
  281.                 print OUT "$i:\t", $dbline[$i];
  282.                 ($stop,$action) = split(/\0/, $dbline{$i});
  283.                 print OUT "  break if (", $stop, ")\n" 
  284.                 if $stop;
  285.                 print OUT "  action:  ", $action, "\n" 
  286.                 if $action;
  287.                 last if $signal;
  288.             }
  289.             }
  290.             next CMD; };
  291.         $cmd =~ /^b\b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
  292.             $subname = $1;
  293.             $cond = $2 || '1';
  294.             $subname = "$package'" . $subname unless $subname =~ /'/;
  295.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  296.             ($filename,$i) = split(/[:-]/, $sub{$subname});
  297.             if ($i) {
  298.             *dbline = "_<$filename";
  299.             ++$i while $dbline[$i] == 0 && $i < $#dbline;
  300.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  301.             } else {
  302.             print OUT "Subroutine $subname not found.\n";
  303.             }
  304.             next CMD; };
  305.         $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
  306.             $i = ($1?$1:$line);
  307.             $cond = $2 || '1';
  308.             if ($dbline[$i] == 0) {
  309.             print OUT "Line $i not breakable.\n";
  310.             } else {
  311.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  312.             }
  313.             next CMD; };
  314.         $cmd =~ /^d\b\s*(\d+)?/ && do {
  315.             $i = ($1?$1:$line);
  316.             $dbline{$i} =~ s/^[^\0]*//;
  317.             delete $dbline{$i} if $dbline{$i} eq '';
  318.             next CMD; };
  319.         $cmd =~ /^A$/ && do {
  320.             for ($i = 1; $i <= $max ; $i++) {
  321.             if (defined $dbline{$i}) {
  322.                 $dbline{$i} =~ s/\0[^\0]*//;
  323.                 delete $dbline{$i} if $dbline{$i} eq '';
  324.             }
  325.             }
  326.             next CMD; };
  327.         $cmd =~ /^<\s*(.*)/ && do {
  328.             $pre = do action($1);
  329.             next CMD; };
  330.         $cmd =~ /^>\s*(.*)/ && do {
  331.             $post = do action($1);
  332.             next CMD; };
  333.         $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
  334.             $i = $1;
  335.             if ($dbline[$i] == 0) {
  336.             print OUT "Line $i may not have an action.\n";
  337.             } else {
  338.             $dbline{$i} =~ s/\0[^\0]*//;
  339.             $dbline{$i} .= "\0" . do action($3);
  340.             }
  341.             next CMD; };
  342.         $cmd =~ /^n$/ && do {
  343.             $single = 2;
  344.             $laststep = $cmd;
  345.             last CMD; };
  346.         $cmd =~ /^s$/ && do {
  347.             $single = 1;
  348.             $laststep = $cmd;
  349.             last CMD; };
  350.         $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
  351.             $i = $1;
  352.             if ($i) {
  353.             if ($dbline[$i] == 0) {
  354.                 print OUT "Line $i not breakable.\n";
  355.                 next CMD;
  356.             }
  357.             $dbline{$i} =~ s/(\0|$)/;9$1/;    # add one-time-only b.p.
  358.             }
  359.             for ($i=0; $i <= $#stack; ) {
  360.             $stack[$i++] &= ~1;
  361.             }
  362.             last CMD; };
  363.         $cmd =~ /^r$/ && do {
  364.             $stack[$#stack] |= 2;
  365.             last CMD; };
  366.         $cmd =~ /^T$/ && do {
  367.             local($p,$f,$l,$s,$h,$a,@a,@sub);
  368.             for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  369.             @a = @args;
  370.             for (@a) {
  371.                 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  372.                 $_ = sprintf("%s",$_);
  373.                 }
  374.                 else {
  375.                 s/'/\\'/g;
  376.                 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  377.                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  378.                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  379.                 }
  380.             }
  381.             $w = $w ? '@ = ' : '$ = ';
  382.             $a = $h ? '(' . join(', ', @a) . ')' : '';
  383.             push(@sub, "$w&$s$a from file $f line $l\n");
  384.             last if $signal;
  385.             }
  386.             for ($i=0; $i <= $#sub; $i++) {
  387.             last if $signal;
  388.             print OUT $sub[$i];
  389.             }
  390.             next CMD; };
  391.         $cmd =~ /^\/(.*)$/ && do {
  392.             $inpat = $1;
  393.             $inpat =~ s:([^\\])/$:$1:;
  394.             if ($inpat ne "") {
  395.             eval '$inpat =~ m'."\n$inpat\n";    
  396.             if ($@ ne "") {
  397.                 print OUT "$@";
  398.                 next CMD;
  399.             }
  400.             $pat = $inpat;
  401.             }
  402.             $end = $start;
  403.             eval '
  404.             for (;;) {
  405.             ++$start;
  406.             $start = 1 if ($start > $max);
  407.             last if ($start == $end);
  408.             if ($dbline[$start] =~ m'."\n$pat\n".'i) {
  409.                 if ($emacs) {
  410.                 print OUT "\032\032$filename:$start:0\n";
  411.                 } else {
  412.                 print OUT "$start:\t", $dbline[$start], "\n";
  413.                 }
  414.                 last;
  415.             }
  416.             } ';
  417.             print OUT "/$pat/: not found\n" if ($start == $end);
  418.             next CMD; };
  419.         $cmd =~ /^\?(.*)$/ && do {
  420.             $inpat = $1;
  421.             $inpat =~ s:([^\\])\?$:$1:;
  422.             if ($inpat ne "") {
  423.             eval '$inpat =~ m'."\n$inpat\n";    
  424.             if ($@ ne "") {
  425.                 print OUT "$@";
  426.                 next CMD;
  427.             }
  428.             $pat = $inpat;
  429.             }
  430.             $end = $start;
  431.             eval '
  432.             for (;;) {
  433.             --$start;
  434.             $start = $max if ($start <= 0);
  435.             last if ($start == $end);
  436.             if ($dbline[$start] =~ m'."\n$pat\n".'i) {
  437.                 if ($emacs) {
  438.                 print OUT "\032\032$filename:$start:0\n";
  439.                 } else {
  440.                 print OUT "$start:\t", $dbline[$start], "\n";
  441.                 }
  442.                 last;
  443.             }
  444.             } ';
  445.             print OUT "?$pat?: not found\n" if ($start == $end);
  446.             next CMD; };
  447.         $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
  448.             pop(@hist) if length($cmd) > 1;
  449.             $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
  450.             $cmd = $hist[$i] . "\n";
  451.             print OUT $cmd;
  452.             redo CMD; };
  453.         $cmd =~ /^!(.+)$/ && do {
  454.             $pat = "^$1";
  455.             pop(@hist) if length($cmd) > 1;
  456.             for ($i = $#hist; $i; --$i) {
  457.             last if $hist[$i] =~ $pat;
  458.             }
  459.             if (!$i) {
  460.             print OUT "No such command!\n\n";
  461.             next CMD;
  462.             }
  463.             $cmd = $hist[$i] . "\n";
  464.             print OUT $cmd;
  465.             redo CMD; };
  466.         $cmd =~ /^H\b\s*(-(\d+))?/ && do {
  467.             $end = $2?($#hist-$2):0;
  468.             $hist = 0 if $hist < 0;
  469.             for ($i=$#hist; $i>$end; $i--) {
  470.             print OUT "$i: ",$hist[$i],"\n"
  471.                 unless $hist[$i] =~ /^.?$/;
  472.             };
  473.             next CMD; };
  474.         $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
  475.         $cmd =~ /^=/ && do {
  476.             if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
  477.             $alias{$k}="s~$k~$v~";
  478.             print OUT "$k = $v\n";
  479.             } elsif ($cmd =~ /^=\s*$/) {
  480.             foreach $k (sort keys(%alias)) {
  481.                 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
  482.                 print OUT "$k = $v\n";
  483.                 } else {
  484.                 print OUT "$k\t$alias{$k}\n";
  485.                 };
  486.             };
  487.             };
  488.             next CMD; };
  489.         }
  490.         $evalarg = $cmd; &eval;
  491.         print OUT "\n";
  492.     }
  493.     if ($post) {
  494.         $evalarg = $post; &eval;
  495.     }
  496.     }
  497.     ($@, $!, $[, $,, $/, $\) = @saved;
  498. }
  499.  
  500. sub save {
  501.     @saved = ($@, $!, $[, $,, $/, $\);
  502.     $[ = 0; $, = ""; $/ = "\n"; $\ = "";
  503. }
  504.  
  505. # The following takes its argument via $evalarg to preserve current @_
  506.  
  507. sub eval {
  508.     eval "$usercontext $evalarg; &DB'save";
  509.     print OUT $@;
  510. }
  511.  
  512. sub action {
  513.     local($action) = @_;
  514.     while ($action =~ s/\\$//) {
  515.     print OUT "+ ";
  516.     $action .= &gets;
  517.     }
  518.     $action;
  519. }
  520.  
  521. sub gets {
  522.     local($.);
  523.     <IN>;
  524. }
  525.  
  526. sub catch {
  527.     $signal = 1;
  528. }
  529.  
  530. sub sub {
  531.     push(@stack, $single);
  532.     $single &= 1;
  533.     $single |= 4 if $#stack == $deep;
  534.     if (wantarray) {
  535.     @i = &$sub;
  536.     $single |= pop(@stack);
  537.     @i;
  538.     }
  539.     else {
  540.     $i = &$sub;
  541.     $single |= pop(@stack);
  542.     $i;
  543.     }
  544. }
  545.  
  546. $single = 1;            # so it stops on first executable statement
  547. @hist = ('?');
  548. $SIG{'INT'} = "DB'catch";
  549. $deep = 100;        # warning if stack gets this deep
  550. $window = 10;
  551. $preview = 3;
  552.  
  553. @stack = (0);
  554. @ARGS = @ARGV;
  555. for (@args) {
  556.     s/'/\\'/g;
  557.     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  558. }
  559.  
  560. if (-f '.perldb') {
  561.     do './.perldb';
  562. }
  563. elsif (-f "$ENV{'LOGDIR'}/.perldb") {
  564.     do "$ENV{'LOGDIR'}/.perldb";
  565. }
  566. elsif (-f "$ENV{'HOME'}/.perldb") {
  567.     do "$ENV{'HOME'}/.perldb";
  568. }
  569.  
  570. 1;
  571.